package cn.ecasoft.controller;

import cn.ecasoft.constant.Result;
import cn.ecasoft.service.EnergyConsumptionService;
import cn.ecasoft.util.EncryptUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

/**
 * @Author: LiangChun
 * @Description: 能耗数据
 * @DateTime: 2022/10/25
 **/
@RequestMapping("/energy")
@RestController
public class EnergyConsumptionController {
    @Autowired
    private EnergyConsumptionService energyConsumptionService;

    /**
     *  采集器
     */
    @RequestMapping(value = "/collector",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
    public Result<Object> putCollectorData(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws IOException {
//        return energyConsumptionService.putCollerctorData(file,request);
        return null;
    }


    public static void main(String[] args) {
        Socket socket = null;
        BufferedWriter bufferedWriter=null;
        BufferedReader bufferedReader = null;
        try {
//            socket = new Socket("121.199.19.103", 10911);
            socket = new Socket("127.0.0.1", 8889);
            System.out.println(socket.isConnected());
            bufferedWriter=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            String body ="55 AA 55 AA 94 00 00 00 00 00 00 00 7A 08 71 F1 55 30 56 E8 94 D2 B9 07 52 80 07 4B A6 3E 0A 18 BA 8B C6 FF EB 1E 5E 61 12 52 B6 AC 1C C0 D2 6D B2 F5 C7 97 02 D4 AB 6A A9 F2 D8 74 05 91 C1 45 CC E7 88 B8 46 97 F3 1E 66 15 55 B0 93 E7 E4 67 5A 6B DE 0F C2 7A 94 39 12 CC E9 64 6A E9 BD D2 7C 06 1D 3D 5D 23 CF 1D F2 F9 B1 3D 17 3C 46 12 AA BB 50 E8 66 7D E2 45 58 A3 84 AF 6C AB F2 A6 88 D8 F9 3F 6B D7 63 36 A7 04 8E 65 BD 53 54 12 91 8D 04 27 5A C1 A4 56 EB A8 7A 05 68 68 16 16 \r";
//            bufferedWriter.write("GET / HTTP/1.1\r\n");//注意，必须携带/r,只有碰到换行符，bufferWriter的writeLine才会返回
            bufferedWriter.write(new String(EncryptUtil.hex2bytes(body), StandardCharsets.ISO_8859_1));
            bufferedWriter.flush();
            String receiveMsg= bufferedReader.readLine();
            System.out.println("服务端收到消息：" + receiveMsg);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(socket!=null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }




}
